package autocode

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/autocode"
	autoCodeReq "github.com/flipped-aurora/gin-vue-admin/server/model/autocode/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"time"
)

type GoodsInfoService struct {
}

// CreateGoodsInfo 创建GoodsInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (goodsInfoService *GoodsInfoService) CreateGoodsInfo(goodsInfo autocode.GoodsInfo) (err error) {
	err = global.GVA_DB.Create(&goodsInfo).Error
	return err
}

// DeleteGoodsInfo 删除GoodsInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (goodsInfoService *GoodsInfoService) DeleteGoodsInfo(goodsInfo autocode.GoodsInfo) (err error) {
	err = global.GVA_DB.Delete(&goodsInfo).Error
	return err
}

// DeleteGoodsInfoByIds 批量删除GoodsInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (goodsInfoService *GoodsInfoService) DeleteGoodsInfoByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]autocode.GoodsInfo{}, "id in ?", ids.Ids).Error
	return err
}

// UpdateGoodsInfo 更新GoodsInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (goodsInfoService *GoodsInfoService) UpdateGoodsInfo(goodsInfo autocode.GoodsInfo) (err error) {
	err = global.GVA_DB.Save(&goodsInfo).Error
	return err
}

// GetGoodsInfo 根据id获取GoodsInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (goodsInfoService *GoodsInfoService) GetGoodsInfo(id uint) (err error, goodsInfo autocode.GoodsInfo) {
	err = global.GVA_DB.Where("id = ?", id).First(&goodsInfo).Error
	return
}

// GetGoodsInfoInfoList 分页获取GoodsInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (goodsInfoService *GoodsInfoService) GetGoodsInfoInfoList(info autoCodeReq.GoodsInfoSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&autocode.GoodsInfo{}).Order("is_delete, sour, created_at desc")
	var goodsInfos []autocode.GoodsInfo
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.Type != nil && *info.Type != 0 {
		db = db.Where("type = ?", info.Type)
	}
	if info.GoodsType != nil && *info.GoodsType != 0 {
		db = db.Where("goods_type = ?", info.GoodsType)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	err = db.Limit(limit).Offset(offset).Find(&goodsInfos).Error
	return err, goodsInfos, total
}

// GetGoodsInfoListDay 分页获取GoodsInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (goodsInfoService *GoodsInfoService) GetGoodsInfoListDay(info autoCodeReq.GoodsInfoSearchDay) (err error, list interface{}) {
	var goodsInfos []autocode.GoodsInfoDay
	db := global.GVA_DB
	err = db.Raw("select gi.goods_name,gi.id,a1.have_num from goods_info gi left join (select count(*) have_num,g.id from goods_info g left join user_goods_info ug on ug.goods_id = g.id where ug.create_time > ? and  ug.create_time < ? group by g.id) a1 on a1.id =gi.id where gi.goods_type = ?", time.Unix(info.StartData.Unix(), 0), time.Unix(info.EndData.Unix()+86400-1, 0), info.GoodsType).Scan(&goodsInfos).Error
	return err, goodsInfos
	//db := global.GVA_DB.Model(&autocode.GoodsInfo{})
	//db.Joins("left join user_info on user_info.id = user_goods_info.user_id")
	//limit := info.PageSize
	//offset := info.PageSize * (info.Page - 1)
	//// 创建db
	//db := global.GVA_DB.Model(&autocode.GoodsInfo{}).Order("is_delete, sour, created_at desc")
	//var goodsInfos []autocode.GoodsInfo
	//// 如果有条件搜索 下方会自动创建搜索语句
	//if info.Type != nil && *info.Type != 0 {
	//	db = db.Where("type = ?", info.Type)
	//}
	//if info.GoodsType != nil && *info.GoodsType != 0 {
	//	db = db.Where("goods_type = ?", info.GoodsType)
	//}
	//err = db.Count(&total).Error
	//if err != nil {
	//	return
	//}
	//err = db.Limit(limit).Offset(offset).Find(&goodsInfos).Error
	//return err, goodsInfos, total
}
